import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/file.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:hzy_project_template/utils/gallery_tool.dart';
import 'package:hzy_project_template/utils/utils_index.dart';
import "package:universal_html/html.dart" as html;

class ImageUtils {
  /// 删除本地图片数据
  static delectLoachImage(List<String> list) async {
    for (var item in list) {
      DefaultCacheManager().removeFile(item);
    }
  }

  /// 保存图片到本地 数组图片
  static Future<List<String>> savePictureToLocalWithList({
    required List list,
    String? nameType,
    int? imageSizeType = 0,
  }) async {
    List<String> urllist = [];
    for (var item in list) {
      String url = '';
      if (item is String) {
        url = item;
      } else if (item is Uint8List) {
        url = await saveOnlyImage(
          imgthumbBytes: item,
        );
      } else {
        url = await saveOnlyImage(
          data: item,
          nameType: nameType,
          imageSizeType: imageSizeType,
        );
      }
      urllist.add(url);
    }
    return urllist;
  }

  /// 保存单张图片
  /// imagesizetype 0 原图 1 1000 * 1000 2 500 * 500 3 150 * 150
  static saveOnlyImage({
    dynamic data,
    int? imageSizeType = 0,
    Uint8List? imgthumbBytes,
    String? nameType,
  }) async {
    Uint8List? thumbBytes = await GalleryTool.configImageBytesCompress(
      type: imageSizeType!,
      imgthumbBytes: imgthumbBytes,
      data: data,
    );
    String? url;
    if (thumbBytes != null) {
      url = await saveOnlyWithbytes(
        bytes: thumbBytes,
        type: nameType,
      );
    }
    return url;
  }

  /// 最终存图片方法
  static saveOnlyWithbytes({
    required Uint8List bytes,
    String? type,
  }) async {
    String url = configSaveImageName(type: type);

    if (kIsWeb) {
      webSaveOnlyWithBytes(bytes: bytes, type: type);
    } else {
      await DefaultCacheManager().putFile(url, bytes);
    }

    return url;
  }

  static webSaveOnlyWithBytes({
    required Uint8List bytes,
    String? type,
  }) {
    String url = configSaveImageName(type: type);
    final blob = html.Blob([bytes], 'image/png');
    final href = html.Url.createObjectUrlFromBlob(blob);
    final anchor = html.AnchorElement(href: href);
    anchor.download = url;
    anchor.click();
    html.Url.revokeObjectUrl(href);
  }

  // 获取本地图片
  static Future<List<Image?>?> getLoachImage(
    List<String> list, {
    double? w,
    double? h,
    BoxFit? fit,
  }) async {
    List<Image?> images = [];
    for (var item in list) {
      Image? image = await getLoachOnlyImage(
        url: item,
        w: w,
        h: h,
        fit: fit,
      );
      images.add(image);
    }
    return images;
  }

  /// 获取单张本地图片
  static Future<Image?> getLoachOnlyImage({
    required String url,
    double? w,
    double? h,
    BoxFit? fit,
  }) async {
    if (url == "") {
      return null;
    }
    final File file = await getLocahOnlyImageFile(url: url);
    return Image.file(
      file,
      width: w,
      height: h,
      fit: fit,
    );
  }

  /// 获取单张图片本地路径
  static getLocahOnlyImageFile({
    required String url,
  }) async {
    File file = await DefaultCacheManager().getSingleFile(
      url,
    );
    return file;
  }

  /// 获取本地图片file
  static getLocahImagefile(
    List<String> list,
  ) async {
    List<File> files = [];
    for (String item in list) {
      final File file = await getLocahOnlyImageFile(url: item);
      files.add(file);
    }
    return files;
  }

  /// 获取本地图片 bytes
  static getLocahImageBytes({
    required List<String> list,
  }) async {
    List<Uint8List> bytes = [];
    for (String item in list) {
      Uint8List uint8list = await getLocahOnlyImageBytes(url: item);
      bytes.add(uint8list);
    }
    return bytes;
  }

  static getLocahOnlyImageBytes({
    required String url,
  }) async {
    final File file = await getLocahOnlyImageFile(
      url: url,
    );
    Uint8List byte = await file.readAsBytes();
    return byte;
  }

  /// 用于生成图片路径
  /// 明文 = hzy + [类型] + 时间戳
  /// 转换后md5
  ///
  static String configSaveImageName({
    String? type,
  }) {
    int nowtime = currentTimeMillis();
    String imagename = "hzy${(type ?? "")}$nowtime";
    String? mdname = generateMD5(data: imagename);
    return mdname ?? imagename;
  }
}
